<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>作用域</title>
    <script>
      //全局变量 -- 从定义它的位置开始可以任意访问
      let num = 10
      console.log(num)

      function fn() {
        //局部变量 -- 只能在定义它的{}范围内使用
        let a = 5
        console.log(a)

        //注意: 如果函数内部定义了和外部全局变量同名的变量,则访问时,就近原则,
        //即优先访问的是函数内的局部变量
        //let num = 15
        console.log(num)
      }

      //console.log(a) //Uncaught ReferenceError: a is not defined

      fn()

      /* let i = 0
      for (; i < 10; i++) {}
      console.log(i) */
    </script>
  </head>
  <body></body>
</html>
